#if __has_include(<SEGMasonry/SEGMasonry.h>)
#import <SEGMasonry/SEGMasonry.h>
#else
#import "SEGMasonry.h"
#endif

#if __has_include(<SEGTool/UIColor+SEGCategory.h>)
#import <SEGTool/UIColor+SEGCategory.h>
#else
#import "UIColor+SEGCategory.h"
#endif

#import "SEGRootView.h"

@interface SEGRootView()<UITableViewDataSource,UITableViewDelegate>
@end

@implementation SEGRootView

#pragma mark - life cycle
- (instancetype)init {
    if (self == [super init]) {
        [self segInitViews];
        [self bindViewModel];
        [self updateConstraints];
    }
    return self;
}

- (instancetype)initWithViewModel:(SEGRootViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
        [self segInitViews];
        [self updateConstraints];
        [self bindViewModel];
    }
    return self;
}

-(void)dealloc {
    NSLog(@"%s dealloc",__FUNCTION__);
}

#pragma mark - Init Views
-(void)segInitViews{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.mainTableView];
}

#pragma mark - Layout
- (void)updateConstraints {
    [super updateConstraints];
    [self.mainTableView seg_makeConstraints:^(SEGConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
    }];

}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.contentDataSource.count;;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGContentTableViewCell *cell = [SEGContentTableViewCell cellWithTableView:tableView indexPath:indexPath];
    if (self.viewModel.contentDataSource.count > indexPath.row) {
        cell.viewModel = self.viewModel.contentDataSource[indexPath.row];
    }
    return  cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.viewModel.contentDataSource.count > indexPath.row) {
        // SEGContentTableViewCellViewModel *data = self.viewModel.contentDataSource[indexPath.row];
    }
}

#pragma mark - Public Methods
#pragma mark - Private Methods
#pragma mark - RAC Data Binding
- (void)bindViewModel {
    //@weakify(self);

    // {@"code":@(0),@"msg":@""}
    [self.viewModel.messageSubject subscribeNext:^(NSDictionary*  _Nullable x) {
        //@strongify(self);
        NSInteger code = [[x valueForKey:@"code"] integerValue];
        NSString *msg = [x valueForKey:@"msg"];

        switch(code) {
            case 0: 
            NSLog(@"%@",msg);
            break;
            default:
                break;
        }
    }];

    [[[self.viewModel.requestCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];

    [self.viewModel.requestCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];

    // [self.viewModel.requestCommand execute:nil];

    [self seg_networkSubNext];
}

- (void)seg_networkSubNext {
    //@weakify(self);
    [[[self.viewModel.wxCodeToLoginCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.wxCodeToLoginCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.wxCodeToLoginCommand execute:nil];

    [[[self.viewModel.fastRegisterCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.fastRegisterCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.fastRegisterCommand execute:nil];

    [[[self.viewModel.loginByTelCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.loginByTelCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.loginByTelCommand execute:nil];

    [[[self.viewModel.getTelByJgTokenCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.getTelByJgTokenCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.getTelByJgTokenCommand execute:nil];

    [[[self.viewModel.validSmsCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.validSmsCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.validSmsCommand execute:nil];

    [[[self.viewModel.wxBindCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.wxBindCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.wxBindCommand execute:nil];

    [[[self.viewModel.iosBindCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.iosBindCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.iosBindCommand execute:nil];

    [[[self.viewModel.iosCodeToLoginCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.iosCodeToLoginCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.iosCodeToLoginCommand execute:nil];

    [[[self.viewModel.getDefaultCommunityCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.getDefaultCommunityCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.getDefaultCommunityCommand execute:nil];

    [[[self.viewModel.queryCommunityListV2Command executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];
    
    [self.viewModel.queryCommunityListV2Command.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];
    
    // [self.viewModel.queryCommunityListV2Command execute:nil];

}

#pragma mark - getter
- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.backgroundColor = UIColor.clearColor;
        _mainTableView.showsVerticalScrollIndicator = NO;        
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.tableFooterView = [[UIView alloc] init];
        [_mainTableView registerClass:[SEGContentTableViewCell class] forCellReuseIdentifier:[SEGContentTableViewCell reuseIdentifier]];
        if (@available(iOS 11, *)) {
            _mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _mainTableView.estimatedSectionHeaderHeight = 0;
            _mainTableView.estimatedSectionFooterHeight = 0;
        }        
    }
    return _mainTableView;
}

-(SEGRootViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGRootViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark setter
@end
